var $, layer
    , upload;
/**
 * Created by dengzbon 2018/4/20.
 * 附件上传
 */
layui.use(['upload', 'layer'], function () {
    $ = layui.jquery, layer = layui.layer
        , upload = layui.upload;

    var clspath = $('#clspath').val();

    //多文件上传
    var fileListView = $('#fileList')
        , uploadListIns = upload.render({
        elem: '#fileSelect'
        , url: '/iot/manager/upload'
        , accept: 'file'
        , data: {clspath: clspath}
        , size: 51200 //最大上传文件大小为50M
        , multiple: true
        , auto: false
        , bindAction: '#fileUpload'
        , choose: function (obj) {
            $('#fileUpload').removeAttr("disabled");
            var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
            //读取本地文件
            obj.preview(function (index, file, result) {
                var tr = $(['<tr id="upload-' + index + '">'
                    , '<td>' + file.name + '</td>'
                    , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                    , '<td>等待上传</td>'
                    , '<td>'
                    , '<button class="layui-btn layui-btn-xs demo-reload layui-hide"><i class="zkuasgm-icon">&#xe6b0;</i>重传</button>'
                    , '<button class="layui-btn layui-btn-danger layui-btn-xs demo-delete"><i class="zkuasgm-icon">&#xe66d;</i>删除</button>'
                    , '</td>'
                    , '</tr>'].join(''));

                //单个重传
                tr.find('.demo-reload').on('click', function () {
                    obj.upload(index, file);
                });

                //删除
                tr.find('.demo-delete').on('click', function () {
                    delete files[index]; //删除对应的文件
                    tr.remove();
                    uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                });

                fileListView.append(tr);
            });
        }
        , before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            //预读本地文件示例，不支持ie8
            layer.load(1); //上传loading
        }
        , done: function (res, index, upload) {
            $('#fileUpload').attr("disabled", "disabled");
            layer.closeAll('loading'); //关闭loading
            if (res.code == 200) { //上传成功
                var tr = fileListView.find('tr#upload-' + index)
                    , tds = tr.children();
                if ($("#projectId").val()) {
                    addOperation($("#projectId").val(), "上传了文件" + res.data.clspath);
                }
                tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                return delete this.files[index]; //删除文件队列已经上传成功的文件
            }
            if (res.code == 300) { //上失败
                layer.msg(res.msg, {shift: 6, icon: 2});
                return delete this.files[index]; //删除文件队列已经上传成功的文件
            }
            this.error(index, upload);
        }
        , error: function (index, upload) {
            layer.closeAll('loading'); //关闭loading
            var tr = fileListView.find('tr#upload-' + index)
                , tds = tr.children();
            tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
        }
    });
});

/**
 * 下载附件
 * @param fileId
 */
function downloadFile(fileId, e) {
    layer.load(1); //上传loading
    var downloadUrl = "/base/file/download/";
    if (fileId != null && $.trim(fileId) != "") {
        var myWindow = window.open(downloadUrl + fileId);
        myWindow.document.title = "下载附件";
        if ($("#projectId").val()) {
            addOperation($("#projectId").val(), "下载了附件" + $(e).closest("tr").find("td:first").text());
        }
        // window.location = downloadUrl + fileId;
    } else {
        layer.msg("参数错误", {icon: 5, offset: 'rb', area: ['120px', '80px'], anim: 2});
    }
    layer.closeAll('loading'); //关闭loading
}

/**
 * 删除附件
 * @param fileId
 */
function deleteFile(fileId, e) {
    if (fileId == null || $.trim(fileId) == "") {
        layer.msg("参数错误", {icon: 5, offset: 'rb', area: ['120px', '80px'], anim: 2});
        return null;
    }
    layer.confirm("确定删除选中附件吗", function (el) {
        layer.load(1); //上传loading
        $.ajax({
            url: '/base/file/delete/' + fileId,
            type: 'post',
            dataType: 'json',
            async: false, traditional: true,
            success: function (d) {
                if (d.code == 800200) {
                    layer.alert("删除附件成功");
                    if ($("#projectId").val()) {
                        addOperation($("#projectId").val(), "删除了附件" + $(e).closest("tr").find("td:first").text());
                    }
                    $(e).parent().parent().remove();
                } else {
                    layer.msg(d.msg, {shift: 6, icon: 2});
                }
            },
            complete: function () {
                layer.closeAll('loading'); //关闭loading
            }, error: function () {
                layer.msg('请求失败', {icon: 2});
            }
        });
    })
}

/**
 * 预览图片
 * @param fileId
 */
function showImage(fileId) {
    if (fileId == null || fileId == '' || fileId == undefined) {
        layer.msg('图片无效', {icon: 2});
        return null;
    }
    $("#imageView").attr("src", "/base/file/getImage/" + fileId);

    layer.open({
        type: 1,
        title: false,
        closeBtn: 1,
        skin: 'layui-layer-nobg', //没有背景色
        shadeClose: true,
        scrollbar: false,
        content: "<img id=\"imageView\" style=\"width: 500px; height: 500px;\" src=\"/base/file/getImage/"+fileId+"\" class=\"layui-layer-wrap\">",
        area: ['auto', 'auto'],
        success: function () {
        }
    });


};
